//
//  main.cpp
//  ThreadAccumulate
//
//  Created by huge on 2021/7/30.
//  Copyright © 2021 huge. All rights reserved.
//

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <sys/time.h>
#include <thread>

using namespace std;

//#define nothread 1

#ifdef nothread
 // 单线程版
//耗时 ：start：s: 1627613638, ms: 1627613638164 s: 1627613644, ms: 1627613644873   duration of time: -6709
 
int GetSum(vector<int>::iterator first,vector<int>::iterator last)
{
    return  accumulate(first,last,0);//调用C++标准库算法
}

int main()
{
    struct timeval time;
    // 获取时间，理论到us
 
    gettimeofday(&time, NULL);
    printf("s: %ld, ms: %ld\n", time.tv_sec, (time.tv_sec*1000 + time.tv_usec/1000));
    long startTime = time.tv_sec*1000 +time.tv_usec/1000;

    vector<int> largeArrays;
    for(int i=0;i<100000000;i++)
    {
        if(i%2==0)
            largeArrays.push_back(i);
        else
            largeArrays.push_back(-1*i);
    }
    int res = GetSum(largeArrays.begin(),largeArrays.end());
    
    
    // 重新获取
    gettimeofday(&time, NULL);
    printf("s: %ld, ms: %ld\n", time.tv_sec, (time.tv_sec*1000 + time.tv_usec/1000));
    long endTime = time.tv_sec*1000 +time.tv_usec/1000;
    cout << "single sum: " << res<< "\t" << "duration of time: " << startTime-endTime << endl;

    return 0;
}

#else

//多线程版
//耗时 start:s: 1627613706, ms: 1627613706589 s: 1627613712, ms: 1627613712255 duration of time: -5666
//线程要做的事情就写在这个线程函数中
void GetSumT(vector<int>::iterator first,vector<int>::iterator last,int &result)
{
    result = accumulate(first,last,0); //调用C++标准库算法
}

int main() //主线程
{
    struct timeval time;
    // 获取时间，理论到us
    
    gettimeofday(&time, NULL);
    printf("s: %ld, ms: %ld\n", time.tv_sec, (time.tv_sec*1000 + time.tv_usec/1000));
    long startTime = time.tv_sec*1000 +time.tv_usec/1000;

    
    int result1,result2,result3,result4,result5;
    vector<int> largeArrays;
    for(int i=0;i<100000000;i++)
    {
        if(i%2==0)
            largeArrays.push_back(i);
        else
            largeArrays.push_back(-1*i);
    }
    thread first(GetSumT,largeArrays.begin(),
                 largeArrays.begin()+20000000,std::ref(result1)); //子线程1
    thread second(GetSumT,largeArrays.begin()+20000000,
                  largeArrays.begin()+40000000,std::ref(result2)); //子线程2
    thread third(GetSumT,largeArrays.begin()+40000000,
                 largeArrays.begin()+60000000,std::ref(result3)); //子线程3
    thread fouth(GetSumT,largeArrays.begin()+60000000,
                 largeArrays.begin()+80000000,std::ref(result4)); //子线程4
    thread fifth(GetSumT,largeArrays.begin()+80000000,
                 largeArrays.end(),std::ref(result5)); //子线程5
    
    first.join(); //主线程要等待子线程执行完毕
    second.join();
    third.join();
    fouth.join();
    fifth.join();
    
    int resultSum = result1+result2+result3+result4+result5; //汇总各个子线程的结果
   
    
    // 重新获取
    gettimeofday(&time, NULL);
    printf("s: %ld, ms: %ld\n", time.tv_sec, (time.tv_sec*1000 + time.tv_usec/1000));
    
    long endTime = time.tv_sec*1000 +time.tv_usec/1000;
    cout << "several thread sum: " << resultSum << "\t" << "duration of time: " << startTime-endTime << endl;

    
    return 0;
}

#endif
